package com.kreedx.article.factory;

import com.geccocrawler.gecco.GeccoEngine;
import com.geccocrawler.gecco.listener.EventListener;
import com.geccocrawler.gecco.spring.SpringGeccoEngine;
import com.kreedx.article.factory.pipeline.ArticlePipeline;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

@EqualsAndHashCode(callSuper = true)
@Component
@EnableScheduling
@Data
public class MyGeccoRunner extends SpringGeccoEngine {
    @Value("${gecco.canRun:#{true }}")
    private boolean canRun;
    @Override
    public void init() {
        fetch();
    }

    void fetch(){
        if(canRun){
            GeccoEngine.create()
                    .classpath("com.kreedx.article.factory")
                    .setEventListener(new EventListener() {
                        @Override
                        public void onStart(GeccoEngine geccoEngine) {

                            canRun = false;
                            ArticlePipeline.fetchedSet.clear();
                        }

                        @Override
                        public void onPause(GeccoEngine geccoEngine) {

                        }

                        @Override
                        public void onRestart(GeccoEngine geccoEngine) {

                        }

                        @Override
                        public void onStop(GeccoEngine geccoEngine) {
                            canRun = true;
                        }
                    })
                    .pipelineFactory(springPipelineFactory)
//                .start("https://www.jianshu.com/")
                    .start("https://news.qq.com/")
                    .thread(1)
                    .loop(false)
                    .start();
        }
    }


    @Scheduled(cron = "0 0 * * * ?")
    void doScratch(){
        fetch();
    }
}
